package com.midea.ai.air.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.midea.ai.air.R;

/**
 * Created by ouyangdi on 2016/4/21.
 * 连接家电时圆形波纹扩散View
 */
public class CircleWaveView extends ImageView {
    public static final int ANIMATOR_TIME = 900; //一次动画执行时间
    private float centerX, centerY; //圆心的坐标
    private Paint paintWave;  //画笔
    private float maxRadius; //最大半径
    private float minRadius; //最小半径
    private boolean isAnimation = false; //是否正在执行动画
    private float lineWidth;
    private AnimatorSet animatorSet;
    private float floatRadius1,floatRadius2,floatRadius3;

    public CircleWaveView(Context context) {
        this(context, null, 0);
    }

    public CircleWaveView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        minRadius = getResources().getDimension(R.dimen.height_160dp)/2.0f;
        lineWidth = getResources().getDimension(R.dimen.height_1dp);
        maxRadius = getResources().getDimension(R.dimen.height_205dp)/2.0f;

        paintWave = new Paint();
        paintWave.setColor(getResources().getColor(R.color.air_device_ac));
        paintWave.setAntiAlias(true);
        paintWave.setStyle(Paint.Style.STROKE);
        paintWave.setStrokeWidth(lineWidth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = (float) w/2f;
        centerY = (float) h/2f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(isAnimation) {
            drawCircle(floatRadius1, canvas);
            drawCircle(floatRadius2, canvas);
            drawCircle(floatRadius3, canvas);
        }
    }

    private void drawCircle(float floatRadius, Canvas canvas) {
        int alpha = (int) (255 * (1.0f - floatRadius / maxRadius));
        if (alpha <= 0) {
            return;
        }
        paintWave.setAlpha(alpha);
        canvas.drawCircle(centerX, centerY, floatRadius, paintWave);
    }

    public void setFloatRadius1(float floatRadius1) {
        this.floatRadius1 = floatRadius1;
        invalidate();
    }

    public void setFloatRadius2(float floatRadius2) {
        this.floatRadius2 = floatRadius2;
        invalidate();
    }

    public void setFloatRadius3(float floatRadius3) {
        this.floatRadius3 = floatRadius3;
        invalidate();
    }

    /**
     * 设置波浪颜色
     * @param waveColor 波浪颜色
     */
    public void setWaveColor(int waveColor) {
        paintWave.setColor(waveColor);
    }

    /**
     * 开始动画
     */
    public void startAnimatorSet() {
        if(animatorSet == null) {
            ObjectAnimator anim1 = createAnim("floatRadius1", 0);
            ObjectAnimator anim2 = createAnim("floatRadius2", ANIMATOR_TIME/3);
            ObjectAnimator anim3 = createAnim("floatRadius3", 2*ANIMATOR_TIME/3);

            animatorSet = new AnimatorSet();
            animatorSet.playTogether(anim1, anim2, anim3);
        }
        isAnimation = true;
        animatorSet.start();
    }

    @NonNull
    private ObjectAnimator createAnim(String floatRadius, long startDelay) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(this, floatRadius, minRadius, maxRadius);
        anim.setDuration(ANIMATOR_TIME);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(ObjectAnimator.INFINITE);
        anim.setRepeatMode(ObjectAnimator.RESTART);
        anim.setStartDelay(startDelay);
        return anim;
    }

    /**
     * 停止动画
     */
    public void stopAnimatorSet() {
        if(animatorSet != null) {
            animatorSet.end();
            isAnimation = false;
        }
    }
}
